package com.tg.paw.ums.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tg.paw.common.constant.CommonConstant;
import com.tg.paw.ums.dto.LoginAccountDto;
import com.tg.paw.ums.entity.Admin;
import com.tg.paw.ums.entity.Permission;
import com.tg.paw.ums.params.LoginAccountParam;
import com.tg.paw.ums.service.*;
import com.tg.paw.common.util.JWTUtil;
import com.tg.paw.common.util.TokenData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Rubble
 * @date 2020/10/28
 */
@Slf4j
@Service
public class ApiLoginServiceImpl  implements IApiLoginService {


    @Value("${jwt.secretKey:123456}")
    private String secretKey;

    @Autowired
    private IAdminService adminService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private IAdminRoleRelationService adminRoleRelationService;
    @Autowired
    private IRolePermissionRelationService rolePermissionRelationService;
    @Autowired
    private IPermissionService permissionService;

    @Override
    public LoginAccountDto loginByAccount(LoginAccountParam param) {
        Assert.notNull(param.getUserName(),"用户名不能为空");
        Assert.notNull(param.getPassword(),"密码不能为空");

        LoginAccountDto dto = new LoginAccountDto();
        dto.setType(param.getType().name());

        Admin admin = adminService.getByLoginUserName(param.getUserName());
        if(admin!=null){
            boolean matches = passwordEncoder.matches(param.getPassword(), admin.getPassword());
            if(matches){
                TokenData tokenData = JWTUtil.generateToken(admin.getAdminId(), param.getUserName(),secretKey);
                dto.setTokenData(tokenData);
//                获取用户权限
                List<String> authorities = getAdminAuthorities(admin.getAdminId());
                dto.setAuthorities(authorities);
            }else {
                dto.setMessage("用户密码不正确");
            }
        }else {
            dto.setMessage("用户名不存在");
        }
        dto.setStatus(dto.getMessage()==null?"ok":"error");

        return dto;
    }

    @Override
    public List<String> getAdminAuthorities(Long adminId) {
        Admin admin = adminService.getById(adminId);
        List<String> result = new ArrayList<>();
        if(admin!=null){
            if(adminId.longValue()==CommonConstant.ADMIN_ACCOUNT_ID){
                result = Arrays.asList(CommonConstant.ADMIN_AUTHORITY);
            }else {
                List<Permission> userPermissions = permissionService.getUserPermissions(adminId);
                Set<String> set = userPermissions.stream().map(item -> item.getAuthority()).collect(Collectors.toSet());
                result = CollUtil.newArrayList(set);
            }
        }
        return result;
    }

    @Override
    public LoginAccountDto loginByMobile(LoginAccountParam param) {
        Assert.notNull(param.getMobile(),"手机不能为空");
        Assert.notNull(param.getCaptcha(),"验证码不能为空");

        LoginAccountDto dto = new LoginAccountDto();
        dto.setType(param.getType().name());
        dto.setStatus("ok");

        return dto;
    }
}
